package com.jiwei.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 季伟
 * @date 2024/4/2
 */
@Configuration
@EnableAsync
public class TaskExecutorConfig {
    @Bean("redisStatisticsExecutor")
    public TaskExecutor RedisStatisticsExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(0);
        executor.setMaxPoolSize(Integer.MAX_VALUE);
        executor.setKeepAliveSeconds(0);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("redis-statistics-async-");
        return executor;
    }
    @Bean(destroyMethod = "shutdown")
    public ExecutorService executorService(){
        return new ThreadPoolExecutor(0,Integer.MAX_VALUE,0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());

    }
}
